/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
*/

package com.jinhe.tss.component.dynproperty;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

import com.jinhe.tss.component.dynproperty.entity.DynableEntity;
import com.jinhe.tss.component.dynproperty.entity.GridTemplate;
import com.jinhe.tss.component.dynproperty.entity.PropertyDef;
import com.jinhe.tss.component.dynproperty.entity.XFormTemplate;
import com.jinhe.tss.component.dynproperty.entity.XFormTemplateTab;
import com.jinhe.tss.core.util.EasyUtils;
import com.jinhe.tss.core.web.dispaly.tree.ILevelTreeNode;
import com.jinhe.tss.core.web.dispaly.tree.ITreeNode;
import com.jinhe.tss.core.web.dispaly.tree.ITreeParser;
import com.jinhe.tss.core.web.dispaly.tree.TreeAttributesMap;
import com.jinhe.tss.core.web.dispaly.tree.TreeNode;

/** 
 * <p> DynTreeParser.java </p> 
 * 动态属性树解析器
 * 
 * @author Jon.King 2008/04/14 10:18:53 $ 
 */
public class DynPropertyTreeParser implements ITreeParser {

    public TreeNode parse(Object data) {
        Map<Long, TreeNode> treeNodeMap = new HashMap<Long, TreeNode>();
        TreeNode root = new TreeNode();               
        
        List<?> list = (List<?>)data;
        if(!EasyUtils.isNullOrEmpty(list)){
            Iterator<?> it = list.iterator();
            while(it.hasNext()){
                Object obj = it.next();
                ILevelTreeNode entity = (ILevelTreeNode) obj;
                TreeNode item = new TreeNode(entity);
                treeNodeMap.put(entity.getId(), item);
            }
            
            it =  new LinkedHashSet<Object>(list).iterator(); //用set可去除重复的
            while(it.hasNext()){
                ILevelTreeNode entity = (ILevelTreeNode) it.next();
                Long parentId = entity.getParentId();
                TreeNode parent = (TreeNode) treeNodeMap.get(parentId);
                TreeNode treeNode = (TreeNode) treeNodeMap.get(entity.getId());
                
                TreeNode node = createDynTreeNode(treeNode, (DynableEntity) entity);
                if(parentId.equals(new Long(0))){
                    root.addChild(node);
                }
                else if (parent != null){
                    parent.addChild(node);
                }
            }
        }
        return root;
    }         
    
    private TreeNode createDynTreeNode(TreeNode treeNode, final DynableEntity entity){
        if(entity.isGroup()) return treeNode;
        
        TreeNode xformTemplatesNode = new TreeNode(new ITreeNode(){
            public TreeAttributesMap getAttributes() { 
                TreeAttributesMap map = new TreeAttributesMap("-1" + entity.getId(), "表单布局");
                map.put("type", DynConstants.XFORM_TYPE);
                map.put("icon", "images/form_layout.gif");
                return map;
            }
        });
        TreeNode gridTemplatesNode = new TreeNode(new ITreeNode(){
            public TreeAttributesMap getAttributes() { 
                TreeAttributesMap map = new TreeAttributesMap("-2" + entity.getId(), "列表布局"); 
                map.put("type", DynConstants.GRID_TYPE);
                map.put("icon", "images/list_layout.gif");
                return map;
            }
        });
        
        for(XFormTemplate template : entity.getXformTemplates()){
            TreeNode xformTemplateNode = new TreeNode(template);
            for(XFormTemplateTab tab : template.getTabs()){
                xformTemplateNode.addChild(new TreeNode(tab));
            }
            xformTemplatesNode.addChild(xformTemplateNode);
        }
        
        for(GridTemplate gridTemplate : entity.getGridTemplates()){
            gridTemplatesNode.addChild(new TreeNode(gridTemplate));
        }
        treeNode.addChild(gridTemplatesNode);
        treeNode.addChild(xformTemplatesNode);
        
        for(PropertyDef propertyDef : entity.getProperties()){
            treeNode.addChild(new TreeNode(propertyDef));
        }
        return treeNode;
    }

}

